﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMLogistics.Model;
using Dapper;
using DapperExtensions;

namespace WMLogistics_Dao
{
    public class CompanyDao
    {
        public int AddCompany(wuliu_company_base company)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into wuliu_company_base(");
                strSql.Append("city_id,company_no,company_name,city_name,address,balance,state,bind_url,wm_file_path,wm_file_uri,fk,fk_info,bid_sel,address_display,price_mode,nopickup_price,empty_price,bidoff_price,contacts,information,cont_phone,cont_tel,logo,certificate3,certificate2,certificate1,choice_time,fee_level,signed_status)");
                strSql.Append(" values (");
                strSql.Append("@city_id,@company_no,@company_name,@city_name,@address,@balance,@state,@bind_url,@wm_file_path,@wm_file_uri,@fk,@fk_info,@bid_sel,@address_display,@price_mode,@nopickup_price,@empty_price,@bidoff_price,@contacts,@information,@cont_phone,@cont_tel,@logo,@certificate3,@certificate2,@certificate1,@choice_time,@fee_level,@signed_status)");
                return conn.Execute(strSql.ToString(), company);
            }
        }

        public int ModifyComapny(wuliu_company_base model)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                StringBuilder strSql = new StringBuilder();
                StringBuilder str = new StringBuilder();
                strSql.Append("update wuliu_company_base set ");
                if (model.city_id.IfNotNull())
                    str.Append("city_id=@city_id,");
                //if (model.company_no.IfNotNull())
                //   str.Append("company_no=@company_no,");
                if (model.company_name.IfNotNull())
                    str.Append("company_name=@company_name,");
                if (model.city_name.IfNotNull())
                    str.Append("city_name=@city_name,");
                if (model.address.IfNotNull())
                    str.Append("address=@address,");
                if (model.balance.IfNotNull())
                    str.Append("balance=@balance,");
                if (model.state.IfNotNull())
                    str.Append("state=@state,");
                if (model.bind_url.IfNotNull())
                    str.Append("bind_url=@bind_url,");
                if (model.wm_file_path.IfNotNull())
                    str.Append("wm_file_path=@wm_file_path,");
                if (model.wm_file_uri.IfNotNull())
                    str.Append("wm_file_uri=@wm_file_uri,");
                if (model.fk.IfNotNull())
                    str.Append("fk=@fk,");
                if (model.fk_info.IfNotNull())
                    str.Append("fk_info=@fk_info,");
                if (model.bid_sel.IfNotNull())
                    str.Append("bid_sel=@bid_sel,");
                if (model.address_display.IfNotNull())
                    str.Append("address_display=@address_display,");
                if (model.price_mode.IfNotNull())
                    str.Append("price_mode=@price_mode,");
                if (model.nopickup_price.IfNotNull())
                    str.Append("nopickup_price=@nopickup_price,");
                if (model.empty_price.IfNotNull())
                    str.Append("empty_price=@empty_price,");
                if (model.bidoff_price.IfNotNull())
                    str.Append("bidoff_price=@bidoff_price,");
                if (model.contacts.IfNotNull())
                    str.Append("contacts=@contacts,");
                if (model.information.IfNotNull())
                    str.Append("information=@information,");
                if (model.cont_phone.IfNotNull())
                    str.Append("cont_phone=@cont_phone,");
                if (model.cont_tel.IfNotNull())
                    str.Append("cont_tel=@cont_tel,");
                if (model.logo.IfNotNull())
                    str.Append("logo=@logo,");
                if (model.certificate3.IfNotNull())
                    str.Append("certificate3=@certificate3,");
                if (model.certificate2.IfNotNull())
                    str.Append("certificate2=@certificate2,");
                if (model.certificate1.IfNotNull())
                    str.Append("certificate1=@certificate1,");
                if (model.choice_time.IfNotNull())
                    str.Append("choice_time=@choice_time,");
                if (model.fee_level.IfNotNull())
                    str.Append("fee_level=@fee_level,");
                if (model.signed_status.IfNotNull())
                    str.Append("signed_status=@signed_status,");
                if (model.city_id_x.IfNotNull())
                    str.Append("city_id_x=@city_id_x,");
                strSql.Append(str.ToString().TrimEnd(','));
                strSql.Append(" where company_id=@company_id ");
                return conn.Execute(strSql.ToString(), model);
            }
        }

        public int GetCompanyIdByAccountId(int account_id)
        {
            string sql = "select company_id from wuliu_account_base where account_id=@account_id";
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.ExecuteScalar<int>(sql, new { account_id });
            }
        }
        public wuliu_company_base GetCompanyById(int company_id)
        {
           
            using (var conn = ConnectionHelper.OpenConnection())
            {
                try
                {
                    return conn.Get<wuliu_company_base>(company_id);
                }
                catch (InvalidOperationException ex)
                {
                    throw new Exception("没有该账号的公司信息!", ex);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

        //public wuliu_company_base GetCompanyById(int company_id)
        //{
        //    string sql = "select * from wuliu_company_base where company_id=@company_id";
        //    using (var conn = ConnectionHelper.OpenConnection())
        //    {
        //        try
        //        {
        //            return conn.Query<wuliu_company_base>(sql, new { company_id }).First();
        //        }
        //        catch (InvalidOperationException ex)
        //        {
        //            throw new Exception("没有该账号的公司信息!", ex);
        //        }
        //        catch (Exception ex)
        //        {
        //            throw;
        //        }
        //    }
        //}

        public List<wuliu_company_base> GetCompanyByAccountId(int account_id)
        {
            string sql = "select * from wuliu_company_base where company_id in (select company_id from wuliu_account_base where account_id=@account_id)";
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Query<wuliu_company_base>(sql, new { account_id }).ToList();
            }
        }

        public bool AddSale(wuliu_company_sales mod)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Insert<wuliu_company_sales>(mod)>0;
            }
        }

        public List<wuliu_company_sales> GetSalesByCompanyId(int company_id)
        {
            string sql = "select * from wuliu_company_sales where company_id =@company_id";
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Query<wuliu_company_sales>(sql, new { company_id }).ToList();
            }
        }
     
    }
}
